#!/usr/bin/python
# -*- coding:utf-8 -*-
#scriptName OF3D_zchPipePublicClass.py
#
#import OF3D_PublicClass.OF3D_zchPipePublicClass as OF3D_zchPipePublicClass
#reload(OF3D_zchPipePublicClass)

import maya.cmds as mc
import maya.mel as mel
import maya.OpenMaya as OpenMaya
import time
import sys,re,os
import random

import attributes
reload(attributes)

import fileDialog
reload(fileDialog)

import ikspline
reload(ikspline)

import mayaPrint
reload(mayaPrint)

import objectName
reload(objectName)

import uiAbout
reload(uiAbout)

import pointOnCurve
reload(pointOnCurve)

import searchByName
reload(searchByName)

import shapeName
reload(shapeName)

import mesh
reload(mesh)

import qualityAssurance
reload(qualityAssurance)

import hierarchy
reload(hierarchy)

import strings
reload(strings)

class PublicClass( 
        attributes.AttributeManage ,
        strings.Strings,
        fileDialog.FDialog,
        ikspline.IkSpline,
        mayaPrint.MayaPrint ,
        objectName.Naming,
        pointOnCurve.PointOnCurve,
        searchByName.Matching,
        uiAbout.UIAbout,
        shapeName.ShapeName,
        mesh.MeshShape,
        qualityAssurance.QualityAssurance,
        hierarchy.Hierarchy):
    
    def __init__(self):
        qualityAssurance.QualityAssurance.__init__(self)
        mayaPrint.MayaPrint.__init__(self)
        
        self.mayaVersion = mc.about(f=True)
        self.userName = os.environ['USERNAME']
        
    def dltGeoAndAddToBlendshape(self,baseGeo,targetName,itsBlends=None):
        
        #baseGeo = self.headGeo
        itsSkin=None
        getSkinCluster = mc.ls(mc.listHistory(baseGeo),type="skinCluster")
        if getSkinCluster!=[]:
            for ski in getSkinCluster:
                mc.setAttr("%s.envelope"%(ski),0)
        getBlendShape = mc.ls(mc.listHistory(baseGeo),type="blendShape")
        if getBlendShape!=[]:
            for bls in getBlendShape:
                mc.setAttr("%s.envelope"%(bls),0)
        line = mc.duplicate(baseGeo,name=targetName)[0]
        #set Attribute vis=0 unlock channels 
        mc.setAttr("%s.visibility"%(line),0)
        for atr in ["tx","ty","tz","rx","ry","rz","sx","sy","sz"]:
            mc.setAttr("%s.%s"%(line,atr),lock=False)
        if getSkinCluster!=[]:
            for ski in getSkinCluster:
                mc.setAttr("%s.envelope"%(ski),1)
        if getBlendShape!=[]:
            for bls in getBlendShape:
                mc.setAttr("%s.envelope"%(bls),1)
        shapeOrig = self.removeShapeOrig(line)
        if itsBlends!=None:
            counts = mc.blendShape(itsBlends,q=1,wc=1)
            #whatIs bsMultiIndexForTarget //run it in mel scripts edit
            mi = mel.eval("""bsMultiIndexForTarget("%s",%d)"""%(itsBlends,counts-1))
            if mi==-1:
                mi = counts
            else:
                mi = mi+1
            mc.blendShape(itsBlends,e=1,target=[baseGeo,mi+1,line,1])
        mc.select(cl=True)
        return line
        
    def makeWithSamePlane(self,aJointList,aimVector=[1,0,0],upVector=[0,-1,0]):
        
        if len(aJointList)!=3 and type(aJointList)!=list:
            self.mayaError("no 1 list with 3 names.")
            return
        mc.parent(aJointList[1],aJointList[2],w=True)
        tempGrp = mc.createNode("transform",name="OF3D_orientJontUp_grp")
        mc.delete(mc.pointConstraint(aJointList[0],aJointList[2],tempGrp,offset=[0,0,0],weight=1))
        aimCon = mc.aimConstraint(aJointList[2],aJointList[1],offset=[0,0,0],weight=1,aimVector=aimVector,upVector=upVector,worldUpType="object",worldUpObject=tempGrp)
        mc.delete(aimCon)
        aimCon = mc.aimConstraint(aJointList[1],aJointList[0],offset=[0,0,0],weight=1,aimVector=aimVector,upVector=upVector,worldUpType="object",worldUpObject=tempGrp)
        mc.delete(aimCon)
        mc.makeIdentity(aJointList[0],apply=1,t=0,r=1,s=0,n=0)
        mc.makeIdentity(aJointList[1],apply=1,t=0,r=1,s=0,n=0)
        mc.parent(aJointList[2],aJointList[1])
        mc.parent(aJointList[1],aJointList[0])
        mc.delete(tempGrp)

    def removeUnusedInfluence(self,skinClusterName,printer=True):
        """remove skinCluster Unused Influence 
        """
        amount = 0
        infls = mc.skinCluster(skinClusterName,q=True,inf=True)
        wtinfs = mc.skinCluster(skinClusterName,q=True,wi=True)
        #have no use for runing down.
        if infls==None or wtinfs==None or infls==wtinfs:
            return
        maxVal = len(infls)-len(wtinfs)
        if maxVal<=0:
            return
        #Funtion Main---------------------------------------
        mc.progressWindow(title='',progress=amount,status='Remove Unused Influence...',isInterruptable=True,maxValue=maxVal)
        nodeState = mc.getAttr("%s.nodeState"%(skinClusterName))
        lock = mc.getAttr("%s.nodeState"%(skinClusterName),lock=1)
        mc.setAttr("%s.nodeState"%(skinClusterName),1)
        mc.setAttr("%s.nodeState"%(skinClusterName),lock=False)
        for jnt in infls:
            if mc.progressWindow( query=True, isCancelled=True ) :
                break
            if jnt not in wtinfs:
                mc.skinCluster(skinClusterName,e=True,ri=jnt)
                mc.progressWindow(e=True,progress=amount)
                amount += 1
                if printer:
                    print "skinCluster -e -ri %s %s;"%(jnt,skinClusterName)
        mc.progressWindow(endProgress=1)
        mc.setAttr("%s.nodeState"%(skinClusterName),nodeState)
        mc.setAttr("%s.nodeState"%(skinClusterName),lock=lock)
        
PubCls = PublicClass()
